﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using CommonSite.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class PositionController : BaseAPIController
    {
        public ActionResult Query(string type)
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.sys_Position
                             select new Position
                             {
                                 Id = CryptHelper.Encrypt(p.Id),
                                 PositionName = p.Name
                             }).ToList();
                switch (type)
                {
                    case "combox":
                        return Success(query);

                    default:
                        return Success(new
                        {
                            Total = query.Count,
                            Rows = query
                        });
                }
            }
        }

        public ActionResult Create(string PositionName)
        {
            if (!PositionName.IsNullOrWhiteSpace())
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        if (db.sys_Position.Where(k => k.Name == PositionName)
                              .Count() > 0)
                        {
                            throw new ArgumentException("职位名称不能重复");
                        }

                        db.sys_Position.Insert(() => new sys_Position
                        {
                            Name = PositionName
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                    //return APIResult.Failed;
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Update(Position Position)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int id = CryptHelper.DecryptToInt32(Position.Id);
                        db.sys_Position.Where(k => k.Id == id)
                          .Set(k => k.Name, Position.PositionName)
                          .Update();

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                    //return APIResult.Failed;
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(Position Position)
        {
            try
            {
                using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                {
                    db.BeginTransaction();
                    int id = CryptHelper.DecryptToInt32(Position.Id);
                    if (db.sys_Position.Where(k => k.Id == id && Position.PositionName == k.Name)
                          .Count() == 0)
                    {
                        throw new ArgumentException("职位不存在");
                    }

                    db.sys_UserOrgPosition.Where(k => k.PositionId == id)
                      .Delete();
                    db.sys_Position.Where(k => k.Id == id)
                      .Delete();

                    db.CommitTransaction();
                    return Success();
                }
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }
    }
}
